blob: 6759dbd8a58490936bbc408a748bdfcd88f37862 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15 vector synopsis
16
17namespace std
18{
19
Howard Hinnant324bb032010-08-22 00:02:4320template <class T, class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:1621class vector
Howard Hinnant324bb032010-08-22 00:02:4322{
23public:
24 typedef T value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:1625 typedef Allocator allocator_type;
26 typedef typename allocator_type::reference reference;
27 typedef typename allocator_type::const_reference const_reference;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef typename allocator_type::size_type size_type;
31 typedef typename allocator_type::difference_type difference_type;
32 typedef typename allocator_type::pointer pointer;
33 typedef typename allocator_type::const_pointer const_pointer;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36
Howard Hinnantd1d27a42011-06-03 19:40:4037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantbc8d3f92010-05-11 19:42:1640 explicit vector(size_type n);
Marshall Clowa49a2c92013-09-14 00:47:5941 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:1642 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
43 template <class InputIterator>
44 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
45 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:4046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1648 vector(initializer_list<value_type> il);
49 vector(initializer_list<value_type> il, const allocator_type& a);
50 ~vector();
51 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:4052 vector& operator=(vector&& x)
53 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:0054 allocator_type::propagate_on_container_move_assignment::value ||
55 allocator_type::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:1656 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnantd1d27a42011-06-03 19:40:4062 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1663
Howard Hinnantd1d27a42011-06-03 19:40:4064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1668
Howard Hinnantd1d27a42011-06-03 19:40:4069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1673
Howard Hinnantd1d27a42011-06-03 19:40:4074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1678
Howard Hinnantd1d27a42011-06-03 19:40:4079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1683 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:4084 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1685
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnantd1d27a42011-06-03 19:40:4096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1698
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
Marshall Clow4e42dc92017-01-24 23:09:12102 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnantd1d27a42011-06-03 19:40:40116 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnantd1d27a42011-06-03 19:40:40121 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56122 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
123 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16124
125 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43126};
Howard Hinnantbc8d3f92010-05-11 19:42:16127
Howard Hinnant324bb032010-08-22 00:02:43128template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16129class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43130{
131public:
132 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16133 typedef Allocator allocator_type;
134 typedef implementation-defined iterator;
135 typedef implementation-defined const_iterator;
136 typedef typename allocator_type::size_type size_type;
137 typedef typename allocator_type::difference_type difference_type;
138 typedef iterator pointer;
139 typedef const_iterator const_pointer;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142
143 class reference
144 {
145 public:
Howard Hinnantd1d27a42011-06-03 19:40:40146 reference(const reference&) noexcept;
147 operator bool() const noexcept;
148 reference& operator=(const bool x) noexcept;
149 reference& operator=(const reference& x) noexcept;
150 iterator operator&() const noexcept;
151 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnantd1d27a42011-06-03 19:40:40157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16160 };
161
Howard Hinnantd1d27a42011-06-03 19:40:40162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31164 explicit vector(const allocator_type&);
Marshall Clowa49a2c92013-09-14 00:47:59165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16167 template <class InputIterator>
168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16172 vector(initializer_list<value_type> il);
173 vector(initializer_list<value_type> il, const allocator_type& a);
174 ~vector();
175 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40176 vector& operator=(vector&& x)
177 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00178 allocator_type::propagate_on_container_move_assignment::value ||
179 allocator_type::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnantd1d27a42011-06-03 19:40:40186 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16187
Howard Hinnantd1d27a42011-06-03 19:40:40188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16192
Howard Hinnantd1d27a42011-06-03 19:40:40193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16197
Howard Hinnantd1d27a42011-06-03 19:40:40198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16202
Howard Hinnantd1d27a42011-06-03 19:40:40203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16207 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40208 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clow4e42dc92017-01-24 23:09:12221 template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16222 void pop_back();
223
Marshall Clow198a2a52013-08-13 23:54:12224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnantd1d27a42011-06-03 19:40:40234 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnantd1d27a42011-06-03 19:40:40239 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56240 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
241 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantd1d27a42011-06-03 19:40:40242 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16243
244 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43245};
Howard Hinnantbc8d3f92010-05-11 19:42:16246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255
Howard Hinnantd1d27a42011-06-03 19:40:40256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16259
260} // std
261
262*/
263
264#include <__config>
Eric Fiselierb3792282016-02-20 00:19:45265#include <iosfwd> // for forward declaration of vector
Howard Hinnantbc8d3f92010-05-11 19:42:16266#include <__bit_reference>
267#include <type_traits>
268#include <climits>
269#include <limits>
270#include <initializer_list>
271#include <memory>
272#include <stdexcept>
273#include <algorithm>
274#include <cstring>
275#include <__split_buffer>
276#include <__functional_base>
Howard Hinnantbc8d3f92010-05-11 19:42:16277
Howard Hinnant66c6f972011-11-29 16:45:27278#include <__undef_min_max>
279
Eric Fiselierb9536102014-08-10 23:53:08280#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35281
Howard Hinnant08e17472011-10-17 20:05:10282#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16283#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10284#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16285
286_LIBCPP_BEGIN_NAMESPACE_STD
287
288template <bool>
289class __vector_base_common
290{
291protected:
292 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
Marshall Clow14c09a22016-08-25 15:09:01293 _LIBCPP_NORETURN void __throw_length_error() const;
294 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16295};
296
297template <bool __b>
298void
299__vector_base_common<__b>::__throw_length_error() const
300{
Marshall Clow14c09a22016-08-25 15:09:01301 _VSTD::__throw_length_error("vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16302}
303
304template <bool __b>
305void
306__vector_base_common<__b>::__throw_out_of_range() const
307{
Marshall Clow14c09a22016-08-25 15:09:01308 _VSTD::__throw_out_of_range("vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16309}
310
Howard Hinnante9df0a52013-08-01 18:17:34311#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45312#pragma warning( push )
313#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34314#endif // _LIBCPP_MSVC
Eric Fiselier833d6442016-09-15 22:27:07315_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34316#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45317#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34318#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16319
320template <class _Tp, class _Allocator>
321class __vector_base
322 : protected __vector_base_common<true>
323{
324protected:
Howard Hinnant324bb032010-08-22 00:02:43325 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16326 typedef _Allocator allocator_type;
327 typedef allocator_traits<allocator_type> __alloc_traits;
328 typedef value_type& reference;
329 typedef const value_type& const_reference;
330 typedef typename __alloc_traits::size_type size_type;
331 typedef typename __alloc_traits::difference_type difference_type;
332 typedef typename __alloc_traits::pointer pointer;
333 typedef typename __alloc_traits::const_pointer const_pointer;
334 typedef pointer iterator;
335 typedef const_pointer const_iterator;
336
337 pointer __begin_;
338 pointer __end_;
339 __compressed_pair<pointer, allocator_type> __end_cap_;
340
Howard Hinnantd1d27a42011-06-03 19:40:40341 _LIBCPP_INLINE_VISIBILITY
342 allocator_type& __alloc() _NOEXCEPT
343 {return __end_cap_.second();}
344 _LIBCPP_INLINE_VISIBILITY
345 const allocator_type& __alloc() const _NOEXCEPT
346 {return __end_cap_.second();}
347 _LIBCPP_INLINE_VISIBILITY
348 pointer& __end_cap() _NOEXCEPT
349 {return __end_cap_.first();}
350 _LIBCPP_INLINE_VISIBILITY
351 const pointer& __end_cap() const _NOEXCEPT
352 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16353
Howard Hinnantd1d27a42011-06-03 19:40:40354 _LIBCPP_INLINE_VISIBILITY
355 __vector_base()
356 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43357 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16358 ~__vector_base();
359
Howard Hinnantd1d27a42011-06-03 19:40:40360 _LIBCPP_INLINE_VISIBILITY
361 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
362 _LIBCPP_INLINE_VISIBILITY
363 size_type capacity() const _NOEXCEPT
364 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16365
Howard Hinnantd1d27a42011-06-03 19:40:40366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32367 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16368
Howard Hinnantee6ccd02010-09-23 18:58:28369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16370 void __copy_assign_alloc(const __vector_base& __c)
371 {__copy_assign_alloc(__c, integral_constant<bool,
372 __alloc_traits::propagate_on_container_copy_assignment::value>());}
373
Howard Hinnantee6ccd02010-09-23 18:58:28374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16375 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40376 _NOEXCEPT_(
377 !__alloc_traits::propagate_on_container_move_assignment::value ||
378 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16379 {__move_assign_alloc(__c, integral_constant<bool,
380 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16381private:
Howard Hinnantee6ccd02010-09-23 18:58:28382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16383 void __copy_assign_alloc(const __vector_base& __c, true_type)
384 {
385 if (__alloc() != __c.__alloc())
386 {
387 clear();
388 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
389 __begin_ = __end_ = __end_cap() = nullptr;
390 }
391 __alloc() = __c.__alloc();
392 }
393
Howard Hinnantee6ccd02010-09-23 18:58:28394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04395 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16396 {}
397
Howard Hinnantee6ccd02010-09-23 18:58:28398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31399 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40400 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16401 {
Howard Hinnant0949eed2011-06-30 21:18:19402 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16403 }
404
Howard Hinnantee6ccd02010-09-23 18:58:28405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04406 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40407 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16408 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16409};
410
411template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16413void
Howard Hinnant2c39cbe2013-06-27 19:35:32414__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16415{
Bruce Mitchener50145cc2017-03-23 14:39:23416 pointer __soon_to_be_end = __end_;
417 while (__new_last != __soon_to_be_end)
418 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
419 __end_ = __new_last;
Howard Hinnantbc8d3f92010-05-11 19:42:16420}
421
422template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00423inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16424__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40425 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32426 : __begin_(nullptr),
427 __end_(nullptr),
428 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16429{
430}
431
432template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00433inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16434__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32435 : __begin_(nullptr),
436 __end_(nullptr),
437 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16438{
439}
440
441template <class _Tp, class _Allocator>
442__vector_base<_Tp, _Allocator>::~__vector_base()
443{
Howard Hinnant2c39cbe2013-06-27 19:35:32444 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16445 {
446 clear();
447 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
448 }
449}
450
Eric Fiselierb3792282016-02-20 00:19:45451template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierc3589a82017-01-04 23:56:00452class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantbc8d3f92010-05-11 19:42:16453 : private __vector_base<_Tp, _Allocator>
454{
455private:
456 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow1f50f2d2014-05-08 14:14:06457 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant324bb032010-08-22 00:02:43458public:
Howard Hinnantbc8d3f92010-05-11 19:42:16459 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43460 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16461 typedef _Allocator allocator_type;
462 typedef typename __base::__alloc_traits __alloc_traits;
463 typedef typename __base::reference reference;
464 typedef typename __base::const_reference const_reference;
465 typedef typename __base::size_type size_type;
466 typedef typename __base::difference_type difference_type;
467 typedef typename __base::pointer pointer;
468 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16469 typedef __wrap_iter<pointer> iterator;
470 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19471 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
472 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16473
Howard Hinnant02d5e182013-03-26 19:04:56474 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
475 "Allocator::value_type must be same type as value_type");
476
Howard Hinnantd1d27a42011-06-03 19:40:40477 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41478 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51479 {
Howard Hinnantabe26282011-09-16 17:29:17480#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51481 __get_db()->__insert_c(this);
482#endif
483 }
484 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20485#if _LIBCPP_STD_VER <= 14
486 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
487#else
488 _NOEXCEPT
489#endif
Howard Hinnant7a563db2011-09-14 18:33:51490 : __base(__a)
491 {
Howard Hinnantabe26282011-09-16 17:29:17492#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51493 __get_db()->__insert_c(this);
494#endif
495 }
Howard Hinnantbc8d3f92010-05-11 19:42:16496 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59497#if _LIBCPP_STD_VER > 11
498 explicit vector(size_type __n, const allocator_type& __a);
499#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16500 vector(size_type __n, const_reference __x);
501 vector(size_type __n, const_reference __x, const allocator_type& __a);
502 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54503 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16504 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32505 !__is_forward_iterator<_InputIterator>::value &&
506 is_constructible<
507 value_type,
Howard Hinnantde589f22013-09-21 21:13:54508 typename iterator_traits<_InputIterator>::reference>::value,
509 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16510 template <class _InputIterator>
511 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
512 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32513 !__is_forward_iterator<_InputIterator>::value &&
514 is_constructible<
515 value_type,
516 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16517 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54518 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32519 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
520 is_constructible<
521 value_type,
Howard Hinnantde589f22013-09-21 21:13:54522 typename iterator_traits<_ForwardIterator>::reference>::value,
523 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16524 template <class _ForwardIterator>
525 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32526 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
527 is_constructible<
528 value_type,
529 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiselierad421ef2017-04-16 02:40:45530
Howard Hinnantabe26282011-09-16 17:29:17531#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51533 ~vector()
534 {
535 __get_db()->__erase_c(this);
536 }
Howard Hinnantbc8d3f92010-05-11 19:42:16537#endif
538
539 vector(const vector& __x);
540 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16542 vector& operator=(const vector& __x);
Eric Fiselierad421ef2017-04-16 02:40:45543
544#ifndef _LIBCPP_CXX03_LANG
545 _LIBCPP_INLINE_VISIBILITY
546 vector(initializer_list<value_type> __il);
547
548 _LIBCPP_INLINE_VISIBILITY
549 vector(initializer_list<value_type> __il, const allocator_type& __a);
550
Howard Hinnant2d72b1e2010-12-17 14:46:43551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40552 vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32553#if _LIBCPP_STD_VER > 14
554 _NOEXCEPT;
555#else
Howard Hinnantd1d27a42011-06-03 19:40:40556 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32557#endif
Eric Fiselierad421ef2017-04-16 02:40:45558
Howard Hinnant2d72b1e2010-12-17 14:46:43559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16560 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40562 vector& operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00563 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierad421ef2017-04-16 02:40:45564
Howard Hinnantee6ccd02010-09-23 18:58:28565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16566 vector& operator=(initializer_list<value_type> __il)
567 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiselierad421ef2017-04-16 02:40:45568
569#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16570
571 template <class _InputIterator>
572 typename enable_if
573 <
574 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32575 !__is_forward_iterator<_InputIterator>::value &&
576 is_constructible<
577 value_type,
578 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16579 void
580 >::type
581 assign(_InputIterator __first, _InputIterator __last);
582 template <class _ForwardIterator>
583 typename enable_if
584 <
Howard Hinnant742fecb2013-03-28 17:44:32585 __is_forward_iterator<_ForwardIterator>::value &&
586 is_constructible<
587 value_type,
588 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16589 void
590 >::type
591 assign(_ForwardIterator __first, _ForwardIterator __last);
592
593 void assign(size_type __n, const_reference __u);
Eric Fiselierad421ef2017-04-16 02:40:45594
595#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantee6ccd02010-09-23 18:58:28596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16597 void assign(initializer_list<value_type> __il)
598 {assign(__il.begin(), __il.end());}
Eric Fiselierad421ef2017-04-16 02:40:45599#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16600
Howard Hinnantd1d27a42011-06-03 19:40:40601 _LIBCPP_INLINE_VISIBILITY
602 allocator_type get_allocator() const _NOEXCEPT
603 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16604
Howard Hinnantd1d27a42011-06-03 19:40:40605 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
606 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
607 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
608 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16609
Howard Hinnantd1d27a42011-06-03 19:40:40610 _LIBCPP_INLINE_VISIBILITY
611 reverse_iterator rbegin() _NOEXCEPT
612 {return reverse_iterator(end());}
613 _LIBCPP_INLINE_VISIBILITY
614 const_reverse_iterator rbegin() const _NOEXCEPT
615 {return const_reverse_iterator(end());}
616 _LIBCPP_INLINE_VISIBILITY
617 reverse_iterator rend() _NOEXCEPT
618 {return reverse_iterator(begin());}
619 _LIBCPP_INLINE_VISIBILITY
620 const_reverse_iterator rend() const _NOEXCEPT
621 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16622
Howard Hinnantd1d27a42011-06-03 19:40:40623 _LIBCPP_INLINE_VISIBILITY
624 const_iterator cbegin() const _NOEXCEPT
625 {return begin();}
626 _LIBCPP_INLINE_VISIBILITY
627 const_iterator cend() const _NOEXCEPT
628 {return end();}
629 _LIBCPP_INLINE_VISIBILITY
630 const_reverse_iterator crbegin() const _NOEXCEPT
631 {return rbegin();}
632 _LIBCPP_INLINE_VISIBILITY
633 const_reverse_iterator crend() const _NOEXCEPT
634 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16635
Howard Hinnantd1d27a42011-06-03 19:40:40636 _LIBCPP_INLINE_VISIBILITY
637 size_type size() const _NOEXCEPT
638 {return static_cast<size_type>(this->__end_ - this->__begin_);}
639 _LIBCPP_INLINE_VISIBILITY
640 size_type capacity() const _NOEXCEPT
641 {return __base::capacity();}
642 _LIBCPP_INLINE_VISIBILITY
643 bool empty() const _NOEXCEPT
644 {return this->__begin_ == this->__end_;}
645 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16646 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40647 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16648
649 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
650 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
651 reference at(size_type __n);
652 const_reference at(size_type __n) const;
653
Howard Hinnant7a563db2011-09-14 18:33:51654 _LIBCPP_INLINE_VISIBILITY reference front()
655 {
656 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
657 return *this->__begin_;
658 }
659 _LIBCPP_INLINE_VISIBILITY const_reference front() const
660 {
661 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
662 return *this->__begin_;
663 }
664 _LIBCPP_INLINE_VISIBILITY reference back()
665 {
666 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
667 return *(this->__end_ - 1);
668 }
669 _LIBCPP_INLINE_VISIBILITY const_reference back() const
670 {
671 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
672 return *(this->__end_ - 1);
673 }
Howard Hinnantbc8d3f92010-05-11 19:42:16674
Howard Hinnantd1d27a42011-06-03 19:40:40675 _LIBCPP_INLINE_VISIBILITY
676 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19677 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40678 _LIBCPP_INLINE_VISIBILITY
679 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19680 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16681
Howard Hinnant2d72b1e2010-12-17 14:46:43682 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiselierad421ef2017-04-16 02:40:45683
684#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb0bfd9b2012-02-15 00:41:34685 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiselierad421ef2017-04-16 02:40:45686
Howard Hinnantbc8d3f92010-05-11 19:42:16687 template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13688 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4e42dc92017-01-24 23:09:12689#if _LIBCPP_STD_VER > 14
Eric Fiselier3816ef92016-07-21 03:20:17690 reference emplace_back(_Args&&... __args);
Marshall Clow4e42dc92017-01-24 23:09:12691#else
692 void emplace_back(_Args&&... __args);
693#endif
Eric Fiselierad421ef2017-04-16 02:40:45694#endif // !_LIBCPP_CXX03_LANG
695
Evgeniy Stepanova3b25f82015-11-07 01:22:13696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16697 void pop_back();
698
699 iterator insert(const_iterator __position, const_reference __x);
Eric Fiselierad421ef2017-04-16 02:40:45700
701#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16702 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16703 template <class... _Args>
704 iterator emplace(const_iterator __position, _Args&&... __args);
Eric Fiselierad421ef2017-04-16 02:40:45705#endif // !_LIBCPP_CXX03_LANG
706
Howard Hinnantbc8d3f92010-05-11 19:42:16707 iterator insert(const_iterator __position, size_type __n, const_reference __x);
708 template <class _InputIterator>
709 typename enable_if
710 <
711 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32712 !__is_forward_iterator<_InputIterator>::value &&
713 is_constructible<
714 value_type,
715 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16716 iterator
717 >::type
718 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
719 template <class _ForwardIterator>
720 typename enable_if
721 <
Howard Hinnant742fecb2013-03-28 17:44:32722 __is_forward_iterator<_ForwardIterator>::value &&
723 is_constructible<
724 value_type,
725 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16726 iterator
727 >::type
728 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierad421ef2017-04-16 02:40:45729
730#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantee6ccd02010-09-23 18:58:28731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16732 iterator insert(const_iterator __position, initializer_list<value_type> __il)
733 {return insert(__position, __il.begin(), __il.end());}
Eric Fiselierad421ef2017-04-16 02:40:45734#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16735
Howard Hinnant2d72b1e2010-12-17 14:46:43736 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16737 iterator erase(const_iterator __first, const_iterator __last);
738
Howard Hinnantd1d27a42011-06-03 19:40:40739 _LIBCPP_INLINE_VISIBILITY
740 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51741 {
Marshall Clow1f50f2d2014-05-08 14:14:06742 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51743 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06744 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51745 __invalidate_all_iterators();
746 }
Howard Hinnantbc8d3f92010-05-11 19:42:16747
748 void resize(size_type __sz);
749 void resize(size_type __sz, const_reference __x);
750
Howard Hinnantd1d27a42011-06-03 19:40:40751 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56752#if _LIBCPP_STD_VER >= 14
Eric Fiselierfb342382016-12-28 06:06:09753 _NOEXCEPT_DEBUG;
Marshall Clow7d914d12015-07-13 20:04:56754#else
Eric Fiselierfb342382016-12-28 06:06:09755 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56756 __is_nothrow_swappable<allocator_type>::value);
757#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16758
759 bool __invariants() const;
760
Howard Hinnantabe26282011-09-16 17:29:17761#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51762
763 bool __dereferenceable(const const_iterator* __i) const;
764 bool __decrementable(const const_iterator* __i) const;
765 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
766 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
767
Howard Hinnantabe26282011-09-16 17:29:17768#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51769
Howard Hinnantbc8d3f92010-05-11 19:42:16770private:
Howard Hinnant2d72b1e2010-12-17 14:46:43771 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselierfb342382016-12-28 06:06:09772 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16773 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40774 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43775 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31776 void __construct_at_end(size_type __n);
Evgeniy Stepanova3b25f82015-11-07 01:22:13777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16778 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16779 template <class _ForwardIterator>
780 typename enable_if
781 <
782 __is_forward_iterator<_ForwardIterator>::value,
783 void
784 >::type
Eric Fiselier088ed9f2015-03-31 16:54:19785 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16786 void __append(size_type __n);
787 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40789 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40791 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16792 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
793 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
794 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40795 void __move_assign(vector& __c, true_type)
796 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clowaf961ed2015-08-18 18:57:00797 void __move_assign(vector& __c, false_type)
798 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant7a563db2011-09-14 18:33:51799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32800 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51801 {
Eric Fiselierfb342382016-12-28 06:06:09802 __invalidate_iterators_past(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06803 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51804 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06805 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51806 }
Eric Fiselierad421ef2017-04-16 02:40:45807
808#ifndef _LIBCPP_CXX03_LANG
809 template <class _Up> void __push_back_slow_path(_Up&& __x);
810
Howard Hinnant0438ea22012-02-26 15:30:12811 template <class... _Args>
Eric Fiselierad421ef2017-04-16 02:40:45812 void __emplace_back_slow_path(_Args&&... __args);
813#else
814 template <class _Up> void __push_back_slow_path(_Up& __x);
Howard Hinnant0438ea22012-02-26 15:30:12815#endif
Eric Fiselierad421ef2017-04-16 02:40:45816
Marshall Clow1f50f2d2014-05-08 14:14:06817 // The following functions are no-ops outside of AddressSanitizer mode.
818 // We call annotatations only for the default Allocator because other allocators
819 // may not meet the AddressSanitizer alignment constraints.
820 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow1f50f2d2014-05-08 14:14:06821#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier0e5ebbc2016-12-23 23:37:52822 void __annotate_contiguous_container(const void *__beg, const void *__end,
823 const void *__old_mid,
824 const void *__new_mid) const
825 {
826
Marshall Clow1f50f2d2014-05-08 14:14:06827 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
828 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow1f50f2d2014-05-08 14:14:06829 }
Eric Fiselier0e5ebbc2016-12-23 23:37:52830#else
831 _LIBCPP_INLINE_VISIBILITY
832 void __annotate_contiguous_container(const void*, const void*, const void*,
833 const void*) const {}
834#endif
835 _LIBCPP_INLINE_VISIBILITY
836 void __annotate_new(size_type __current_size) const {
Marshall Clow1f50f2d2014-05-08 14:14:06837 __annotate_contiguous_container(data(), data() + capacity(),
838 data() + capacity(), data() + __current_size);
839 }
Eric Fiselier0e5ebbc2016-12-23 23:37:52840
841 _LIBCPP_INLINE_VISIBILITY
842 void __annotate_delete() const {
Marshall Clow1f50f2d2014-05-08 14:14:06843 __annotate_contiguous_container(data(), data() + capacity(),
844 data() + size(), data() + capacity());
845 }
Eric Fiselier0e5ebbc2016-12-23 23:37:52846
847 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany497f9122014-09-02 23:43:38848 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06849 {
850 __annotate_contiguous_container(data(), data() + capacity(),
851 data() + size(), data() + size() + __n);
852 }
Eric Fiselier0e5ebbc2016-12-23 23:37:52853
854 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany497f9122014-09-02 23:43:38855 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06856 {
857 __annotate_contiguous_container(data(), data() + capacity(),
858 data() + __old_size, data() + size());
859 }
Marshall Clow26f472d2014-09-03 21:37:43860#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38861 // The annotation for size increase should happen before the actual increase,
862 // but if an exception is thrown after that the annotation has to be undone.
863 struct __RAII_IncreaseAnnotator {
864 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20865 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38866 __v.__annotate_increase(__n);
867 }
868 void __done() { __commit = true; }
869 ~__RAII_IncreaseAnnotator() {
870 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20871 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38872 }
873 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38874 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20875 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38876 };
Marshall Clow26f472d2014-09-03 21:37:43877#else
878 struct __RAII_IncreaseAnnotator {
Eric Fiselier0e5ebbc2016-12-23 23:37:52879 _LIBCPP_INLINE_VISIBILITY
880 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
881 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clow26f472d2014-09-03 21:37:43882 };
883#endif
884
Howard Hinnantbc8d3f92010-05-11 19:42:16885};
886
887template <class _Tp, class _Allocator>
888void
889vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
890{
Marshall Clow1f50f2d2014-05-08 14:14:06891 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34892 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19893 _VSTD::swap(this->__begin_, __v.__begin_);
894 _VSTD::swap(this->__end_, __v.__end_);
895 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16896 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06897 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16898 __invalidate_all_iterators();
899}
900
901template <class _Tp, class _Allocator>
902typename vector<_Tp, _Allocator>::pointer
903vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
904{
Marshall Clow1f50f2d2014-05-08 14:14:06905 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16906 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34907 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
908 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19909 _VSTD::swap(this->__begin_, __v.__begin_);
910 _VSTD::swap(this->__end_, __v.__end_);
911 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16912 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06913 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16914 __invalidate_all_iterators();
915 return __r;
916}
917
918// Allocate space for __n objects
919// throws length_error if __n > max_size()
920// throws (probably bad_alloc) if memory run out
921// Precondition: __begin_ == __end_ == __end_cap() == 0
922// Precondition: __n > 0
923// Postcondition: capacity() == __n
924// Postcondition: size() == 0
925template <class _Tp, class _Allocator>
926void
927vector<_Tp, _Allocator>::allocate(size_type __n)
928{
929 if (__n > max_size())
930 this->__throw_length_error();
931 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
932 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06933 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16934}
935
936template <class _Tp, class _Allocator>
937void
Howard Hinnantd1d27a42011-06-03 19:40:40938vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16939{
Howard Hinnant2c39cbe2013-06-27 19:35:32940 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16941 {
942 clear();
943 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32944 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16945 }
946}
947
948template <class _Tp, class _Allocator>
949typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40950vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16951{
Eric Fiselieref3060e2016-11-23 01:18:56952 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
953 numeric_limits<difference_type>::max());
Howard Hinnantbc8d3f92010-05-11 19:42:16954}
955
956// Precondition: __new_size > capacity()
957template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16959typename vector<_Tp, _Allocator>::size_type
960vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
961{
962 const size_type __ms = max_size();
963 if (__new_size > __ms)
964 this->__throw_length_error();
965 const size_type __cap = capacity();
966 if (__cap >= __ms / 2)
967 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58968 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16969}
970
971// Default constructs __n objects starting at __end_
972// throws if construction throws
973// Precondition: __n > 0
974// Precondition: size() + __n <= capacity()
975// Postcondition: size() == size() + __n
976template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16977void
978vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
979{
Howard Hinnantbc8d3f92010-05-11 19:42:16980 allocator_type& __a = this->__alloc();
981 do
982 {
Kostya Serebryany497f9122014-09-02 23:43:38983 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19984 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16985 ++this->__end_;
986 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38987 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16988 } while (__n > 0);
989}
990
Howard Hinnantbc8d3f92010-05-11 19:42:16991// Copy constructs __n objects starting at __end_ from __x
992// throws if construction throws
993// Precondition: __n > 0
994// Precondition: size() + __n <= capacity()
995// Postcondition: size() == old size() + __n
996// Postcondition: [i] == __x for all i in [size() - __n, __n)
997template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13998inline
Howard Hinnantbc8d3f92010-05-11 19:42:16999void
1000vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1001{
Howard Hinnantbc8d3f92010-05-11 19:42:161002 allocator_type& __a = this->__alloc();
1003 do
1004 {
Kostya Serebryany497f9122014-09-02 23:43:381005 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:191006 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161007 ++this->__end_;
1008 --__n;
Kostya Serebryany497f9122014-09-02 23:43:381009 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161010 } while (__n > 0);
1011}
1012
1013template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161014template <class _ForwardIterator>
1015typename enable_if
1016<
1017 __is_forward_iterator<_ForwardIterator>::value,
1018 void
1019>::type
Eric Fiselier088ed9f2015-03-31 16:54:191020vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:161021{
1022 allocator_type& __a = this->__alloc();
Eric Fiselier088ed9f2015-03-31 16:54:191023 __RAII_IncreaseAnnotator __annotator(*this, __n);
1024 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1025 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161026}
1027
1028// Default constructs __n objects starting at __end_
1029// throws if construction throws
1030// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:401031// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:161032template <class _Tp, class _Allocator>
1033void
1034vector<_Tp, _Allocator>::__append(size_type __n)
1035{
1036 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1037 this->__construct_at_end(__n);
1038 else
1039 {
1040 allocator_type& __a = this->__alloc();
1041 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1042 __v.__construct_at_end(__n);
1043 __swap_out_circular_buffer(__v);
1044 }
1045}
1046
1047// Default constructs __n objects starting at __end_
1048// throws if construction throws
1049// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:401050// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:161051template <class _Tp, class _Allocator>
1052void
1053vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1054{
1055 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1056 this->__construct_at_end(__n, __x);
1057 else
1058 {
1059 allocator_type& __a = this->__alloc();
1060 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1061 __v.__construct_at_end(__n, __x);
1062 __swap_out_circular_buffer(__v);
1063 }
1064}
1065
1066template <class _Tp, class _Allocator>
1067vector<_Tp, _Allocator>::vector(size_type __n)
1068{
Howard Hinnant0442b122011-09-16 18:41:291069#if _LIBCPP_DEBUG_LEVEL >= 2
1070 __get_db()->__insert_c(this);
1071#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161072 if (__n > 0)
1073 {
1074 allocate(__n);
1075 __construct_at_end(__n);
1076 }
1077}
1078
Marshall Clowa49a2c92013-09-14 00:47:591079#if _LIBCPP_STD_VER > 11
1080template <class _Tp, class _Allocator>
1081vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1082 : __base(__a)
1083{
1084#if _LIBCPP_DEBUG_LEVEL >= 2
1085 __get_db()->__insert_c(this);
1086#endif
1087 if (__n > 0)
1088 {
1089 allocate(__n);
1090 __construct_at_end(__n);
1091 }
1092}
1093#endif
1094
Howard Hinnantbc8d3f92010-05-11 19:42:161095template <class _Tp, class _Allocator>
1096vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1097{
Howard Hinnant0442b122011-09-16 18:41:291098#if _LIBCPP_DEBUG_LEVEL >= 2
1099 __get_db()->__insert_c(this);
1100#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161101 if (__n > 0)
1102 {
1103 allocate(__n);
1104 __construct_at_end(__n, __x);
1105 }
1106}
1107
1108template <class _Tp, class _Allocator>
1109vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1110 : __base(__a)
1111{
Howard Hinnant0442b122011-09-16 18:41:291112#if _LIBCPP_DEBUG_LEVEL >= 2
1113 __get_db()->__insert_c(this);
1114#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161115 if (__n > 0)
1116 {
1117 allocate(__n);
1118 __construct_at_end(__n, __x);
1119 }
1120}
1121
1122template <class _Tp, class _Allocator>
1123template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:541124vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:161125 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321126 !__is_forward_iterator<_InputIterator>::value &&
1127 is_constructible<
1128 value_type,
Howard Hinnantde589f22013-09-21 21:13:541129 typename iterator_traits<_InputIterator>::reference>::value,
1130 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:161131{
Howard Hinnantabe26282011-09-16 17:29:171132#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511133 __get_db()->__insert_c(this);
1134#endif
Howard Hinnant0442b122011-09-16 18:41:291135 for (; __first != __last; ++__first)
1136 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:161137}
1138
1139template <class _Tp, class _Allocator>
1140template <class _InputIterator>
1141vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1142 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321143 !__is_forward_iterator<_InputIterator>::value &&
1144 is_constructible<
1145 value_type,
1146 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:161147 : __base(__a)
1148{
Howard Hinnantabe26282011-09-16 17:29:171149#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511150 __get_db()->__insert_c(this);
1151#endif
Howard Hinnant0442b122011-09-16 18:41:291152 for (; __first != __last; ++__first)
1153 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:161154}
1155
1156template <class _Tp, class _Allocator>
1157template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:541158vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:321159 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1160 is_constructible<
1161 value_type,
Howard Hinnantde589f22013-09-21 21:13:541162 typename iterator_traits<_ForwardIterator>::reference>::value,
1163 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:161164{
Howard Hinnant0442b122011-09-16 18:41:291165#if _LIBCPP_DEBUG_LEVEL >= 2
1166 __get_db()->__insert_c(this);
1167#endif
Howard Hinnant0949eed2011-06-30 21:18:191168 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:161169 if (__n > 0)
1170 {
1171 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:191172 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161173 }
1174}
1175
1176template <class _Tp, class _Allocator>
1177template <class _ForwardIterator>
1178vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:321179 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1180 is_constructible<
1181 value_type,
1182 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:161183 : __base(__a)
1184{
Howard Hinnant0442b122011-09-16 18:41:291185#if _LIBCPP_DEBUG_LEVEL >= 2
1186 __get_db()->__insert_c(this);
1187#endif
Howard Hinnant0949eed2011-06-30 21:18:191188 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:161189 if (__n > 0)
1190 {
1191 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:191192 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161193 }
1194}
1195
1196template <class _Tp, class _Allocator>
1197vector<_Tp, _Allocator>::vector(const vector& __x)
1198 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1199{
Howard Hinnant0442b122011-09-16 18:41:291200#if _LIBCPP_DEBUG_LEVEL >= 2
1201 __get_db()->__insert_c(this);
1202#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161203 size_type __n = __x.size();
1204 if (__n > 0)
1205 {
1206 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:191207 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161208 }
1209}
1210
1211template <class _Tp, class _Allocator>
1212vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1213 : __base(__a)
1214{
Howard Hinnant0442b122011-09-16 18:41:291215#if _LIBCPP_DEBUG_LEVEL >= 2
1216 __get_db()->__insert_c(this);
1217#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161218 size_type __n = __x.size();
1219 if (__n > 0)
1220 {
1221 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:191222 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161223 }
1224}
1225
Eric Fiselierad421ef2017-04-16 02:40:451226#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161227
1228template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161230vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:321231#if _LIBCPP_STD_VER > 14
1232 _NOEXCEPT
1233#else
Howard Hinnantd1d27a42011-06-03 19:40:401234 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:321235#endif
Howard Hinnant0949eed2011-06-30 21:18:191236 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:161237{
Howard Hinnantabe26282011-09-16 17:29:171238#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511239 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:291240 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:511241#endif
Howard Hinnant0442b122011-09-16 18:41:291242 this->__begin_ = __x.__begin_;
1243 this->__end_ = __x.__end_;
1244 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:321245 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:161246}
1247
1248template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001249inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161250vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1251 : __base(__a)
1252{
Howard Hinnant0442b122011-09-16 18:41:291253#if _LIBCPP_DEBUG_LEVEL >= 2
1254 __get_db()->__insert_c(this);
1255#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161256 if (__a == __x.__alloc())
1257 {
1258 this->__begin_ = __x.__begin_;
1259 this->__end_ = __x.__end_;
1260 this->__end_cap() = __x.__end_cap();
1261 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:291262#if _LIBCPP_DEBUG_LEVEL >= 2
1263 __get_db()->swap(this, &__x);
1264#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161265 }
1266 else
1267 {
Howard Hinnant99968442011-11-29 18:15:501268 typedef move_iterator<iterator> _Ip;
1269 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161270 }
1271}
1272
Howard Hinnantbc8d3f92010-05-11 19:42:161273template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001274inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161275vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1276{
Howard Hinnant0442b122011-09-16 18:41:291277#if _LIBCPP_DEBUG_LEVEL >= 2
1278 __get_db()->__insert_c(this);
1279#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161280 if (__il.size() > 0)
1281 {
1282 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:191283 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:161284 }
1285}
1286
1287template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001288inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161289vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1290 : __base(__a)
1291{
Howard Hinnant0442b122011-09-16 18:41:291292#if _LIBCPP_DEBUG_LEVEL >= 2
1293 __get_db()->__insert_c(this);
1294#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161295 if (__il.size() > 0)
1296 {
1297 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:191298 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:161299 }
1300}
1301
Howard Hinnantbc8d3f92010-05-11 19:42:161302template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161304vector<_Tp, _Allocator>&
1305vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:001306 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:161307{
1308 __move_assign(__x, integral_constant<bool,
1309 __alloc_traits::propagate_on_container_move_assignment::value>());
1310 return *this;
1311}
1312
1313template <class _Tp, class _Allocator>
1314void
1315vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clowaf961ed2015-08-18 18:57:001316 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161317{
1318 if (__base::__alloc() != __c.__alloc())
1319 {
Howard Hinnant99968442011-11-29 18:15:501320 typedef move_iterator<iterator> _Ip;
1321 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161322 }
1323 else
1324 __move_assign(__c, true_type());
1325}
1326
1327template <class _Tp, class _Allocator>
1328void
1329vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:401330 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161331{
1332 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:131333 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:161334 this->__begin_ = __c.__begin_;
1335 this->__end_ = __c.__end_;
1336 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:161337 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:291338#if _LIBCPP_DEBUG_LEVEL >= 2
1339 __get_db()->swap(this, &__c);
1340#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161341}
1342
Eric Fiselierad421ef2017-04-16 02:40:451343#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161344
1345template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161347vector<_Tp, _Allocator>&
1348vector<_Tp, _Allocator>::operator=(const vector& __x)
1349{
1350 if (this != &__x)
1351 {
1352 __base::__copy_assign_alloc(__x);
1353 assign(__x.__begin_, __x.__end_);
1354 }
1355 return *this;
1356}
1357
1358template <class _Tp, class _Allocator>
1359template <class _InputIterator>
1360typename enable_if
1361<
1362 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321363 !__is_forward_iterator<_InputIterator>::value &&
1364 is_constructible<
1365 _Tp,
1366 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161367 void
1368>::type
1369vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1370{
1371 clear();
1372 for (; __first != __last; ++__first)
1373 push_back(*__first);
1374}
1375
1376template <class _Tp, class _Allocator>
1377template <class _ForwardIterator>
1378typename enable_if
1379<
Howard Hinnant742fecb2013-03-28 17:44:321380 __is_forward_iterator<_ForwardIterator>::value &&
1381 is_constructible<
1382 _Tp,
1383 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161384 void
1385>::type
1386vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1387{
Eric Fiselier088ed9f2015-03-31 16:54:191388 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1389 if (__new_size <= capacity())
Howard Hinnantbc8d3f92010-05-11 19:42:161390 {
1391 _ForwardIterator __mid = __last;
1392 bool __growing = false;
Eric Fiselier088ed9f2015-03-31 16:54:191393 if (__new_size > size())
Howard Hinnantbc8d3f92010-05-11 19:42:161394 {
1395 __growing = true;
1396 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:191397 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:161398 }
Howard Hinnant0949eed2011-06-30 21:18:191399 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:161400 if (__growing)
Eric Fiselier088ed9f2015-03-31 16:54:191401 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantbc8d3f92010-05-11 19:42:161402 else
1403 this->__destruct_at_end(__m);
1404 }
1405 else
1406 {
1407 deallocate();
Eric Fiselier088ed9f2015-03-31 16:54:191408 allocate(__recommend(__new_size));
1409 __construct_at_end(__first, __last, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:161410 }
Eric Fiselierfb342382016-12-28 06:06:091411 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:161412}
1413
1414template <class _Tp, class _Allocator>
1415void
1416vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1417{
1418 if (__n <= capacity())
1419 {
1420 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:191421 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:161422 if (__n > __s)
1423 __construct_at_end(__n - __s, __u);
1424 else
Howard Hinnantadff4892010-05-24 17:49:411425 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161426 }
1427 else
1428 {
1429 deallocate();
1430 allocate(__recommend(static_cast<size_type>(__n)));
1431 __construct_at_end(__n, __u);
1432 }
Eric Fiselierfb342382016-12-28 06:06:091433 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:161434}
1435
Howard Hinnant324bb032010-08-22 00:02:431436template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001437inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161438typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401439vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161440{
Howard Hinnantabe26282011-09-16 17:29:171441#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:161442 return iterator(this, __p);
1443#else
1444 return iterator(__p);
1445#endif
1446}
1447
Howard Hinnant324bb032010-08-22 00:02:431448template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001449inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161450typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401451vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161452{
Howard Hinnantabe26282011-09-16 17:29:171453#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:161454 return const_iterator(this, __p);
1455#else
1456 return const_iterator(__p);
1457#endif
1458}
1459
Howard Hinnant324bb032010-08-22 00:02:431460template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161462typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401463vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161464{
1465 return __make_iter(this->__begin_);
1466}
1467
Howard Hinnant324bb032010-08-22 00:02:431468template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001469inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161470typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401471vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161472{
1473 return __make_iter(this->__begin_);
1474}
1475
Howard Hinnant324bb032010-08-22 00:02:431476template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001477inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161478typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401479vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161480{
1481 return __make_iter(this->__end_);
1482}
1483
Howard Hinnant324bb032010-08-22 00:02:431484template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001485inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161486typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401487vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161488{
1489 return __make_iter(this->__end_);
1490}
1491
Howard Hinnant324bb032010-08-22 00:02:431492template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001493inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161494typename vector<_Tp, _Allocator>::reference
1495vector<_Tp, _Allocator>::operator[](size_type __n)
1496{
Howard Hinnant7a563db2011-09-14 18:33:511497 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:161498 return this->__begin_[__n];
1499}
1500
Howard Hinnant324bb032010-08-22 00:02:431501template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161503typename vector<_Tp, _Allocator>::const_reference
1504vector<_Tp, _Allocator>::operator[](size_type __n) const
1505{
Howard Hinnant7a563db2011-09-14 18:33:511506 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:161507 return this->__begin_[__n];
1508}
1509
Howard Hinnant324bb032010-08-22 00:02:431510template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161511typename vector<_Tp, _Allocator>::reference
1512vector<_Tp, _Allocator>::at(size_type __n)
1513{
1514 if (__n >= size())
1515 this->__throw_out_of_range();
1516 return this->__begin_[__n];
1517}
1518
Howard Hinnant324bb032010-08-22 00:02:431519template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161520typename vector<_Tp, _Allocator>::const_reference
1521vector<_Tp, _Allocator>::at(size_type __n) const
1522{
1523 if (__n >= size())
1524 this->__throw_out_of_range();
1525 return this->__begin_[__n];
1526}
1527
1528template <class _Tp, class _Allocator>
1529void
1530vector<_Tp, _Allocator>::reserve(size_type __n)
1531{
1532 if (__n > capacity())
1533 {
1534 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:401535 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:161536 __swap_out_circular_buffer(__v);
1537 }
1538}
1539
1540template <class _Tp, class _Allocator>
1541void
Howard Hinnantd1d27a42011-06-03 19:40:401542vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161543{
1544 if (capacity() > size())
1545 {
1546#ifndef _LIBCPP_NO_EXCEPTIONS
1547 try
1548 {
Howard Hinnant324bb032010-08-22 00:02:431549#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161550 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:401551 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:161552 __swap_out_circular_buffer(__v);
1553#ifndef _LIBCPP_NO_EXCEPTIONS
1554 }
1555 catch (...)
1556 {
1557 }
Howard Hinnant324bb032010-08-22 00:02:431558#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161559 }
1560}
1561
1562template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:341563template <class _Up>
1564void
Eric Fiselierad421ef2017-04-16 02:40:451565#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb0bfd9b2012-02-15 00:41:341566vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1567#else
1568vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1569#endif
1570{
1571 allocator_type& __a = this->__alloc();
1572 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1573 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:591574 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1575 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:341576 __swap_out_circular_buffer(__v);
1577}
1578
1579template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001580inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161581void
1582vector<_Tp, _Allocator>::push_back(const_reference __x)
1583{
Howard Hinnantb0bfd9b2012-02-15 00:41:341584 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:161585 {
Kostya Serebryany497f9122014-09-02 23:43:381586 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161587 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191588 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:381589 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161590 ++this->__end_;
1591 }
1592 else
Howard Hinnantb0bfd9b2012-02-15 00:41:341593 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:161594}
1595
Eric Fiselierad421ef2017-04-16 02:40:451596#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161597
1598template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001599inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161600void
1601vector<_Tp, _Allocator>::push_back(value_type&& __x)
1602{
1603 if (this->__end_ < this->__end_cap())
1604 {
Kostya Serebryany497f9122014-09-02 23:43:381605 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161606 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191607 _VSTD::__to_raw_pointer(this->__end_),
1608 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:381609 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161610 ++this->__end_;
1611 }
1612 else
Howard Hinnantb0bfd9b2012-02-15 00:41:341613 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161614}
1615
Howard Hinnantbc8d3f92010-05-11 19:42:161616template <class _Tp, class _Allocator>
1617template <class... _Args>
1618void
Howard Hinnant0438ea22012-02-26 15:30:121619vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1620{
1621 allocator_type& __a = this->__alloc();
1622 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1623// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:591624 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1625 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:121626 __swap_out_circular_buffer(__v);
1627}
1628
1629template <class _Tp, class _Allocator>
1630template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:131631inline
Marshall Clow4e42dc92017-01-24 23:09:121632#if _LIBCPP_STD_VER > 14
Eric Fiselier3816ef92016-07-21 03:20:171633typename vector<_Tp, _Allocator>::reference
Marshall Clow4e42dc92017-01-24 23:09:121634#else
1635void
1636#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161637vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1638{
1639 if (this->__end_ < this->__end_cap())
1640 {
Kostya Serebryany497f9122014-09-02 23:43:381641 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161642 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191643 _VSTD::__to_raw_pointer(this->__end_),
1644 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:381645 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161646 ++this->__end_;
1647 }
1648 else
Howard Hinnant0438ea22012-02-26 15:30:121649 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clow4e42dc92017-01-24 23:09:121650#if _LIBCPP_STD_VER > 14
Eric Fiselier3816ef92016-07-21 03:20:171651 return this->back();
Marshall Clow4e42dc92017-01-24 23:09:121652#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161653}
1654
Eric Fiselierad421ef2017-04-16 02:40:451655#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161656
1657template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:131658inline
Howard Hinnantbc8d3f92010-05-11 19:42:161659void
1660vector<_Tp, _Allocator>::pop_back()
1661{
Howard Hinnant7a563db2011-09-14 18:33:511662 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:161663 this->__destruct_at_end(this->__end_ - 1);
1664}
1665
1666template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001667inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161668typename vector<_Tp, _Allocator>::iterator
1669vector<_Tp, _Allocator>::erase(const_iterator __position)
1670{
Howard Hinnantabe26282011-09-16 17:29:171671#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511672 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1673 "vector::erase(iterator) called with an iterator not"
1674 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171675#endif
Howard Hinnant782da332013-03-25 22:12:261676 _LIBCPP_ASSERT(__position != end(),
1677 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:321678 difference_type __ps = __position - cbegin();
1679 pointer __p = this->__begin_ + __ps;
Howard Hinnant0949eed2011-06-30 21:18:191680 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselierfb342382016-12-28 06:06:091681 this->__invalidate_iterators_past(__p-1);
1682 iterator __r = __make_iter(__p);
Howard Hinnantbc8d3f92010-05-11 19:42:161683 return __r;
1684}
1685
1686template <class _Tp, class _Allocator>
1687typename vector<_Tp, _Allocator>::iterator
1688vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1689{
Howard Hinnantabe26282011-09-16 17:29:171690#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511691 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1692 "vector::erase(iterator, iterator) called with an iterator not"
1693 " referring to this vector");
Eric Fiselierfb342382016-12-28 06:06:091694 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1695 "vector::erase(iterator, iterator) called with an iterator not"
1696 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171697#endif
Howard Hinnant7a563db2011-09-14 18:33:511698 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:161699 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselierfb342382016-12-28 06:06:091700 if (__first != __last) {
Howard Hinnantb4e67cf2013-04-18 15:02:571701 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselierfb342382016-12-28 06:06:091702 this->__invalidate_iterators_past(__p - 1);
1703 }
1704 iterator __r = __make_iter(__p);
Howard Hinnantbc8d3f92010-05-11 19:42:161705 return __r;
1706}
1707
1708template <class _Tp, class _Allocator>
1709void
1710vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1711{
1712 pointer __old_last = this->__end_;
1713 difference_type __n = __old_last - __to;
1714 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1715 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191716 _VSTD::__to_raw_pointer(this->__end_),
1717 _VSTD::move(*__i));
1718 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:161719}
1720
1721template <class _Tp, class _Allocator>
1722typename vector<_Tp, _Allocator>::iterator
1723vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1724{
Howard Hinnantabe26282011-09-16 17:29:171725#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511726 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1727 "vector::insert(iterator, x) called with an iterator not"
1728 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171729#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161730 pointer __p = this->__begin_ + (__position - begin());
1731 if (this->__end_ < this->__end_cap())
1732 {
Kostya Serebryany497f9122014-09-02 23:43:381733 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161734 if (__p == this->__end_)
1735 {
1736 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191737 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161738 ++this->__end_;
1739 }
1740 else
1741 {
1742 __move_range(__p, this->__end_, __p + 1);
1743 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1744 if (__p <= __xr && __xr < this->__end_)
1745 ++__xr;
1746 *__p = *__xr;
1747 }
Kostya Serebryany497f9122014-09-02 23:43:381748 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161749 }
1750 else
1751 {
1752 allocator_type& __a = this->__alloc();
1753 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1754 __v.push_back(__x);
1755 __p = __swap_out_circular_buffer(__v, __p);
1756 }
1757 return __make_iter(__p);
1758}
1759
Eric Fiselierad421ef2017-04-16 02:40:451760#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161761
1762template <class _Tp, class _Allocator>
1763typename vector<_Tp, _Allocator>::iterator
1764vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1765{
Howard Hinnantabe26282011-09-16 17:29:171766#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511767 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1768 "vector::insert(iterator, x) called with an iterator not"
1769 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171770#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161771 pointer __p = this->__begin_ + (__position - begin());
1772 if (this->__end_ < this->__end_cap())
1773 {
Kostya Serebryany497f9122014-09-02 23:43:381774 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161775 if (__p == this->__end_)
1776 {
1777 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191778 _VSTD::__to_raw_pointer(this->__end_),
1779 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161780 ++this->__end_;
1781 }
1782 else
1783 {
1784 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:191785 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:161786 }
Kostya Serebryany497f9122014-09-02 23:43:381787 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161788 }
1789 else
1790 {
1791 allocator_type& __a = this->__alloc();
1792 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:191793 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161794 __p = __swap_out_circular_buffer(__v, __p);
1795 }
1796 return __make_iter(__p);
1797}
1798
Howard Hinnantbc8d3f92010-05-11 19:42:161799template <class _Tp, class _Allocator>
1800template <class... _Args>
1801typename vector<_Tp, _Allocator>::iterator
1802vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1803{
Howard Hinnantabe26282011-09-16 17:29:171804#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511805 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1806 "vector::emplace(iterator, x) called with an iterator not"
1807 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171808#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161809 pointer __p = this->__begin_ + (__position - begin());
1810 if (this->__end_ < this->__end_cap())
1811 {
Kostya Serebryany497f9122014-09-02 23:43:381812 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161813 if (__p == this->__end_)
1814 {
1815 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191816 _VSTD::__to_raw_pointer(this->__end_),
1817 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161818 ++this->__end_;
1819 }
1820 else
1821 {
Marshall Clow51d7e8e2016-07-11 21:38:081822 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161823 __move_range(__p, this->__end_, __p + 1);
Marshall Clow51d7e8e2016-07-11 21:38:081824 *__p = _VSTD::move(__tmp.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161825 }
Kostya Serebryany497f9122014-09-02 23:43:381826 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161827 }
1828 else
1829 {
1830 allocator_type& __a = this->__alloc();
1831 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:191832 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161833 __p = __swap_out_circular_buffer(__v, __p);
1834 }
1835 return __make_iter(__p);
1836}
1837
Eric Fiselierad421ef2017-04-16 02:40:451838#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161839
1840template <class _Tp, class _Allocator>
1841typename vector<_Tp, _Allocator>::iterator
1842vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1843{
Howard Hinnantabe26282011-09-16 17:29:171844#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511845 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1846 "vector::insert(iterator, n, x) called with an iterator not"
1847 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171848#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161849 pointer __p = this->__begin_ + (__position - begin());
1850 if (__n > 0)
1851 {
1852 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1853 {
1854 size_type __old_n = __n;
1855 pointer __old_last = this->__end_;
1856 if (__n > static_cast<size_type>(this->__end_ - __p))
1857 {
1858 size_type __cx = __n - (this->__end_ - __p);
1859 __construct_at_end(__cx, __x);
1860 __n -= __cx;
1861 }
1862 if (__n > 0)
1863 {
Eric Fiselierd7590952014-11-14 18:28:361864 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161865 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:381866 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161867 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1868 if (__p <= __xr && __xr < this->__end_)
1869 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:191870 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:161871 }
1872 }
1873 else
1874 {
1875 allocator_type& __a = this->__alloc();
1876 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1877 __v.__construct_at_end(__n, __x);
1878 __p = __swap_out_circular_buffer(__v, __p);
1879 }
1880 }
1881 return __make_iter(__p);
1882}
1883
1884template <class _Tp, class _Allocator>
1885template <class _InputIterator>
1886typename enable_if
1887<
1888 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321889 !__is_forward_iterator<_InputIterator>::value &&
1890 is_constructible<
1891 _Tp,
1892 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161893 typename vector<_Tp, _Allocator>::iterator
1894>::type
1895vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1896{
Howard Hinnantabe26282011-09-16 17:29:171897#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511898 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1899 "vector::insert(iterator, range) called with an iterator not"
1900 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171901#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161902 difference_type __off = __position - begin();
1903 pointer __p = this->__begin_ + __off;
1904 allocator_type& __a = this->__alloc();
1905 pointer __old_last = this->__end_;
1906 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1907 {
Eric Fiselier7d439a42015-07-18 18:22:121908 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:191909 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:161910 *__first);
1911 ++this->__end_;
Eric Fiselier7d439a42015-07-18 18:22:121912 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161913 }
1914 __split_buffer<value_type, allocator_type&> __v(__a);
1915 if (__first != __last)
1916 {
1917#ifndef _LIBCPP_NO_EXCEPTIONS
1918 try
1919 {
Howard Hinnant324bb032010-08-22 00:02:431920#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161921 __v.__construct_at_end(__first, __last);
1922 difference_type __old_size = __old_last - this->__begin_;
1923 difference_type __old_p = __p - this->__begin_;
1924 reserve(__recommend(size() + __v.size()));
1925 __p = this->__begin_ + __old_p;
1926 __old_last = this->__begin_ + __old_size;
1927#ifndef _LIBCPP_NO_EXCEPTIONS
1928 }
1929 catch (...)
1930 {
1931 erase(__make_iter(__old_last), end());
1932 throw;
1933 }
Howard Hinnant324bb032010-08-22 00:02:431934#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161935 }
Howard Hinnant0949eed2011-06-30 21:18:191936 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:291937 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1938 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161939 return begin() + __off;
1940}
1941
1942template <class _Tp, class _Allocator>
1943template <class _ForwardIterator>
1944typename enable_if
1945<
Howard Hinnant742fecb2013-03-28 17:44:321946 __is_forward_iterator<_ForwardIterator>::value &&
1947 is_constructible<
1948 _Tp,
1949 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161950 typename vector<_Tp, _Allocator>::iterator
1951>::type
1952vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1953{
Howard Hinnantabe26282011-09-16 17:29:171954#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511955 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1956 "vector::insert(iterator, range) called with an iterator not"
1957 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171958#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161959 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:191960 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:161961 if (__n > 0)
1962 {
1963 if (__n <= this->__end_cap() - this->__end_)
1964 {
1965 size_type __old_n = __n;
1966 pointer __old_last = this->__end_;
1967 _ForwardIterator __m = __last;
1968 difference_type __dx = this->__end_ - __p;
1969 if (__n > __dx)
1970 {
1971 __m = __first;
Eric Fiselier088ed9f2015-03-31 16:54:191972 difference_type __diff = this->__end_ - __p;
1973 _VSTD::advance(__m, __diff);
1974 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantbc8d3f92010-05-11 19:42:161975 __n = __dx;
1976 }
1977 if (__n > 0)
1978 {
Kostya Serebryany497f9122014-09-02 23:43:381979 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161980 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:381981 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:191982 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:161983 }
1984 }
1985 else
1986 {
1987 allocator_type& __a = this->__alloc();
1988 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1989 __v.__construct_at_end(__first, __last);
1990 __p = __swap_out_circular_buffer(__v, __p);
1991 }
1992 }
1993 return __make_iter(__p);
1994}
1995
1996template <class _Tp, class _Allocator>
1997void
1998vector<_Tp, _Allocator>::resize(size_type __sz)
1999{
2000 size_type __cs = size();
2001 if (__cs < __sz)
2002 this->__append(__sz - __cs);
2003 else if (__cs > __sz)
2004 this->__destruct_at_end(this->__begin_ + __sz);
2005}
2006
2007template <class _Tp, class _Allocator>
2008void
2009vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2010{
2011 size_type __cs = size();
2012 if (__cs < __sz)
2013 this->__append(__sz - __cs, __x);
2014 else if (__cs > __sz)
2015 this->__destruct_at_end(this->__begin_ + __sz);
2016}
2017
2018template <class _Tp, class _Allocator>
2019void
2020vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:562021#if _LIBCPP_STD_VER >= 14
Eric Fiselierfb342382016-12-28 06:06:092022 _NOEXCEPT_DEBUG
Marshall Clow7d914d12015-07-13 20:04:562023#else
Eric Fiselierfb342382016-12-28 06:06:092024 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:562025 __is_nothrow_swappable<allocator_type>::value)
2026#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162027{
Howard Hinnant7a563db2011-09-14 18:33:512028 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2029 this->__alloc() == __x.__alloc(),
2030 "vector::swap: Either propagate_on_container_swap must be true"
2031 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:192032 _VSTD::swap(this->__begin_, __x.__begin_);
2033 _VSTD::swap(this->__end_, __x.__end_);
2034 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Eric Fiselierad421ef2017-04-16 02:40:452035 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow7d914d12015-07-13 20:04:562036 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantabe26282011-09-16 17:29:172037#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512038 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:172039#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:162040}
2041
Howard Hinnant324bb032010-08-22 00:02:432042template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162043bool
2044vector<_Tp, _Allocator>::__invariants() const
2045{
Howard Hinnant2c39cbe2013-06-27 19:35:322046 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162047 {
Howard Hinnant2c39cbe2013-06-27 19:35:322048 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162049 return false;
2050 }
2051 else
2052 {
2053 if (this->__begin_ > this->__end_)
2054 return false;
2055 if (this->__begin_ == this->__end_cap())
2056 return false;
2057 if (this->__end_ > this->__end_cap())
2058 return false;
2059 }
2060 return true;
2061}
2062
Howard Hinnantabe26282011-09-16 17:29:172063#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512064
Howard Hinnantbc8d3f92010-05-11 19:42:162065template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:512066bool
2067vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2068{
2069 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2070}
2071
2072template <class _Tp, class _Allocator>
2073bool
2074vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2075{
2076 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2077}
2078
2079template <class _Tp, class _Allocator>
2080bool
2081vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2082{
2083 const_pointer __p = __i->base() + __n;
2084 return this->__begin_ <= __p && __p <= this->__end_;
2085}
2086
2087template <class _Tp, class _Allocator>
2088bool
2089vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2090{
2091 const_pointer __p = __i->base() + __n;
2092 return this->__begin_ <= __p && __p < this->__end_;
2093}
2094
Howard Hinnantabe26282011-09-16 17:29:172095#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512096
2097template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002098inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162099void
2100vector<_Tp, _Allocator>::__invalidate_all_iterators()
2101{
Howard Hinnantabe26282011-09-16 17:29:172102#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512103 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:172104#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:162105}
2106
Eric Fiselierfb342382016-12-28 06:06:092107
2108template <class _Tp, class _Allocator>
2109inline _LIBCPP_INLINE_VISIBILITY
2110void
2111vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2112#if _LIBCPP_DEBUG_LEVEL >= 2
2113 __c_node* __c = __get_db()->__find_c_and_lock(this);
2114 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2115 --__p;
2116 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2117 if (__i->base() > __new_last) {
2118 (*__p)->__c_ = nullptr;
2119 if (--__c->end_ != __p)
2120 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2121 }
2122 }
2123 __get_db()->unlock();
2124#else
2125 ((void)__new_last);
2126#endif
2127}
2128
Howard Hinnantbc8d3f92010-05-11 19:42:162129// vector<bool>
2130
2131template <class _Allocator> class vector<bool, _Allocator>;
2132
2133template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2134
2135template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:232136struct __has_storage_type<vector<bool, _Allocator> >
2137{
2138 static const bool value = true;
2139};
2140
2141template <class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:002142class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162143 : private __vector_base_common<true>
2144{
2145public:
2146 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:432147 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:162148 typedef _Allocator allocator_type;
2149 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:162150 typedef typename __alloc_traits::size_type size_type;
2151 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:382152 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:162153 typedef __bit_iterator<vector, false> pointer;
2154 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:162155 typedef pointer iterator;
2156 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:192157 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2158 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:162159
2160private:
Marshall Clow66302c62015-04-07 05:21:382161 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:162162 typedef allocator_traits<__storage_allocator> __storage_traits;
2163 typedef typename __storage_traits::pointer __storage_pointer;
2164 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2165
2166 __storage_pointer __begin_;
2167 size_type __size_;
2168 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:422169public:
Howard Hinnantf03c3b42011-07-02 20:33:232170 typedef __bit_reference<vector> reference;
2171 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:422172private:
Howard Hinnantd1d27a42011-06-03 19:40:402173 _LIBCPP_INLINE_VISIBILITY
2174 size_type& __cap() _NOEXCEPT
2175 {return __cap_alloc_.first();}
2176 _LIBCPP_INLINE_VISIBILITY
2177 const size_type& __cap() const _NOEXCEPT
2178 {return __cap_alloc_.first();}
2179 _LIBCPP_INLINE_VISIBILITY
2180 __storage_allocator& __alloc() _NOEXCEPT
2181 {return __cap_alloc_.second();}
2182 _LIBCPP_INLINE_VISIBILITY
2183 const __storage_allocator& __alloc() const _NOEXCEPT
2184 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:162185
2186 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2187
Howard Hinnantd1d27a42011-06-03 19:40:402188 _LIBCPP_INLINE_VISIBILITY
2189 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162190 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:402191 _LIBCPP_INLINE_VISIBILITY
2192 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162193 {return (__n - 1) / __bits_per_word + 1;}
2194
2195public:
Howard Hinnantd1d27a42011-06-03 19:40:402196 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:412197 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow127db912015-06-04 00:10:202198
2199 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2200#if _LIBCPP_STD_VER <= 14
2201 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2202#else
2203 _NOEXCEPT;
2204#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162205 ~vector();
2206 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:592207#if _LIBCPP_STD_VER > 11
2208 explicit vector(size_type __n, const allocator_type& __a);
2209#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162210 vector(size_type __n, const value_type& __v);
2211 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2212 template <class _InputIterator>
2213 vector(_InputIterator __first, _InputIterator __last,
2214 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2215 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2216 template <class _InputIterator>
2217 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2218 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2219 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2220 template <class _ForwardIterator>
2221 vector(_ForwardIterator __first, _ForwardIterator __last,
2222 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2223 template <class _ForwardIterator>
2224 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2225 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2226
2227 vector(const vector& __v);
2228 vector(const vector& __v, const allocator_type& __a);
2229 vector& operator=(const vector& __v);
Eric Fiselierad421ef2017-04-16 02:40:452230
2231#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:162232 vector(initializer_list<value_type> __il);
2233 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:162234
Howard Hinnantd1d27a42011-06-03 19:40:402235 _LIBCPP_INLINE_VISIBILITY
2236 vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:322237#if _LIBCPP_STD_VER > 14
2238 _NOEXCEPT;
2239#else
Howard Hinnantd1d27a42011-06-03 19:40:402240 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:322241#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162242 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:402243 _LIBCPP_INLINE_VISIBILITY
2244 vector& operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:002245 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierad421ef2017-04-16 02:40:452246
Howard Hinnantee6ccd02010-09-23 18:58:282247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162248 vector& operator=(initializer_list<value_type> __il)
2249 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiselierad421ef2017-04-16 02:40:452250
2251#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:162252
2253 template <class _InputIterator>
2254 typename enable_if
2255 <
2256 __is_input_iterator<_InputIterator>::value &&
2257 !__is_forward_iterator<_InputIterator>::value,
2258 void
2259 >::type
2260 assign(_InputIterator __first, _InputIterator __last);
2261 template <class _ForwardIterator>
2262 typename enable_if
2263 <
2264 __is_forward_iterator<_ForwardIterator>::value,
2265 void
2266 >::type
2267 assign(_ForwardIterator __first, _ForwardIterator __last);
2268
2269 void assign(size_type __n, const value_type& __x);
Eric Fiselierad421ef2017-04-16 02:40:452270
2271#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantee6ccd02010-09-23 18:58:282272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162273 void assign(initializer_list<value_type> __il)
2274 {assign(__il.begin(), __il.end());}
Eric Fiselierad421ef2017-04-16 02:40:452275#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162276
Howard Hinnantd1d27a42011-06-03 19:40:402277 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162278 {return allocator_type(this->__alloc());}
2279
Howard Hinnantd1d27a42011-06-03 19:40:402280 size_type max_size() const _NOEXCEPT;
2281 _LIBCPP_INLINE_VISIBILITY
2282 size_type capacity() const _NOEXCEPT
2283 {return __internal_cap_to_external(__cap());}
2284 _LIBCPP_INLINE_VISIBILITY
2285 size_type size() const _NOEXCEPT
2286 {return __size_;}
2287 _LIBCPP_INLINE_VISIBILITY
2288 bool empty() const _NOEXCEPT
2289 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:162290 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:402291 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162292
Howard Hinnantd1d27a42011-06-03 19:40:402293 _LIBCPP_INLINE_VISIBILITY
2294 iterator begin() _NOEXCEPT
2295 {return __make_iter(0);}
2296 _LIBCPP_INLINE_VISIBILITY
2297 const_iterator begin() const _NOEXCEPT
2298 {return __make_iter(0);}
2299 _LIBCPP_INLINE_VISIBILITY
2300 iterator end() _NOEXCEPT
2301 {return __make_iter(__size_);}
2302 _LIBCPP_INLINE_VISIBILITY
2303 const_iterator end() const _NOEXCEPT
2304 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:162305
Howard Hinnantd1d27a42011-06-03 19:40:402306 _LIBCPP_INLINE_VISIBILITY
2307 reverse_iterator rbegin() _NOEXCEPT
2308 {return reverse_iterator(end());}
2309 _LIBCPP_INLINE_VISIBILITY
2310 const_reverse_iterator rbegin() const _NOEXCEPT
2311 {return const_reverse_iterator(end());}
2312 _LIBCPP_INLINE_VISIBILITY
2313 reverse_iterator rend() _NOEXCEPT
2314 {return reverse_iterator(begin());}
2315 _LIBCPP_INLINE_VISIBILITY
2316 const_reverse_iterator rend() const _NOEXCEPT
2317 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:162318
Howard Hinnantd1d27a42011-06-03 19:40:402319 _LIBCPP_INLINE_VISIBILITY
2320 const_iterator cbegin() const _NOEXCEPT
2321 {return __make_iter(0);}
2322 _LIBCPP_INLINE_VISIBILITY
2323 const_iterator cend() const _NOEXCEPT
2324 {return __make_iter(__size_);}
2325 _LIBCPP_INLINE_VISIBILITY
2326 const_reverse_iterator crbegin() const _NOEXCEPT
2327 {return rbegin();}
2328 _LIBCPP_INLINE_VISIBILITY
2329 const_reverse_iterator crend() const _NOEXCEPT
2330 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:162331
2332 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2333 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2334 reference at(size_type __n);
2335 const_reference at(size_type __n) const;
2336
2337 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2338 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2339 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2340 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2341
2342 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:122343#if _LIBCPP_STD_VER > 11
2344 template <class... _Args>
Marshall Clow4e42dc92017-01-24 23:09:122345#if _LIBCPP_STD_VER > 14
2346 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2347#else
2348 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2349#endif
2350 {
Eric Fiselier3816ef92016-07-21 03:20:172351 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clow4e42dc92017-01-24 23:09:122352#if _LIBCPP_STD_VER > 14
Eric Fiselier3816ef92016-07-21 03:20:172353 return this->back();
Marshall Clow4e42dc92017-01-24 23:09:122354#endif
Eric Fiselier3816ef92016-07-21 03:20:172355 }
Marshall Clow198a2a52013-08-13 23:54:122356#endif
2357
Howard Hinnantbc8d3f92010-05-11 19:42:162358 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2359
Marshall Clow198a2a52013-08-13 23:54:122360#if _LIBCPP_STD_VER > 11
2361 template <class... _Args>
2362 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2363 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2364#endif
2365
Howard Hinnantbc8d3f92010-05-11 19:42:162366 iterator insert(const_iterator __position, const value_type& __x);
2367 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2368 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2369 template <class _InputIterator>
2370 typename enable_if
2371 <
2372 __is_input_iterator <_InputIterator>::value &&
2373 !__is_forward_iterator<_InputIterator>::value,
2374 iterator
2375 >::type
2376 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2377 template <class _ForwardIterator>
2378 typename enable_if
2379 <
2380 __is_forward_iterator<_ForwardIterator>::value,
2381 iterator
2382 >::type
2383 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierad421ef2017-04-16 02:40:452384
2385#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantee6ccd02010-09-23 18:58:282386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162387 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2388 {return insert(__position, __il.begin(), __il.end());}
Eric Fiselierad421ef2017-04-16 02:40:452389#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162390
Howard Hinnant2d72b1e2010-12-17 14:46:432391 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:162392 iterator erase(const_iterator __first, const_iterator __last);
2393
Howard Hinnantd1d27a42011-06-03 19:40:402394 _LIBCPP_INLINE_VISIBILITY
2395 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:162396
Howard Hinnantd1d27a42011-06-03 19:40:402397 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:562398#if _LIBCPP_STD_VER >= 14
2399 _NOEXCEPT;
2400#else
Eric Fiselierfb342382016-12-28 06:06:092401 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:562402 __is_nothrow_swappable<allocator_type>::value);
2403#endif
Marshall Clow005c60b2016-04-07 14:20:312404 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantbc8d3f92010-05-11 19:42:162405
2406 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:402407 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162408
2409 bool __invariants() const;
2410
2411private:
Howard Hinnant2d72b1e2010-12-17 14:46:432412 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:162413 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:402414 void deallocate() _NOEXCEPT;
2415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:202416 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:422417 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:432418 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2419 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162420 template <class _ForwardIterator>
2421 typename enable_if
2422 <
2423 __is_forward_iterator<_ForwardIterator>::value,
2424 void
2425 >::type
2426 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2427 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:402428 _LIBCPP_INLINE_VISIBILITY
2429 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162430 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:402431 _LIBCPP_INLINE_VISIBILITY
2432 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162433 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:402434 _LIBCPP_INLINE_VISIBILITY
2435 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162436 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:402437 _LIBCPP_INLINE_VISIBILITY
2438 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162439 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:402440 _LIBCPP_INLINE_VISIBILITY
2441 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:322442 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:162443
Howard Hinnantee6ccd02010-09-23 18:58:282444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162445 void __copy_assign_alloc(const vector& __v)
2446 {__copy_assign_alloc(__v, integral_constant<bool,
2447 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:282448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162449 void __copy_assign_alloc(const vector& __c, true_type)
2450 {
2451 if (__alloc() != __c.__alloc())
2452 deallocate();
2453 __alloc() = __c.__alloc();
2454 }
2455
Howard Hinnantee6ccd02010-09-23 18:58:282456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042457 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:162458 {}
2459
2460 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:402461 void __move_assign(vector& __c, true_type)
2462 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:282463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162464 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:402465 _NOEXCEPT_(
2466 !__storage_traits::propagate_on_container_move_assignment::value ||
2467 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162468 {__move_assign_alloc(__c, integral_constant<bool,
2469 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:282470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:312471 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402472 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162473 {
Howard Hinnant0949eed2011-06-30 21:18:192474 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:162475 }
2476
Howard Hinnantee6ccd02010-09-23 18:58:282477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042478 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:402479 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162480 {}
2481
Howard Hinnantd1d27a42011-06-03 19:40:402482 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162483
2484 friend class __bit_reference<vector>;
2485 friend class __bit_const_reference<vector>;
2486 friend class __bit_iterator<vector, false>;
2487 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:182488 friend struct __bit_array<vector>;
Eric Fiselierc3589a82017-01-04 23:56:002489 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:162490};
2491
2492template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002493inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162494void
2495vector<bool, _Allocator>::__invalidate_all_iterators()
2496{
Howard Hinnantbc8d3f92010-05-11 19:42:162497}
2498
2499// Allocate space for __n objects
2500// throws length_error if __n > max_size()
2501// throws (probably bad_alloc) if memory run out
2502// Precondition: __begin_ == __end_ == __cap() == 0
2503// Precondition: __n > 0
2504// Postcondition: capacity() == __n
2505// Postcondition: size() == 0
2506template <class _Allocator>
2507void
2508vector<bool, _Allocator>::allocate(size_type __n)
2509{
2510 if (__n > max_size())
2511 this->__throw_length_error();
2512 __n = __external_cap_to_internal(__n);
2513 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2514 this->__size_ = 0;
2515 this->__cap() = __n;
2516}
2517
2518template <class _Allocator>
2519void
Howard Hinnantd1d27a42011-06-03 19:40:402520vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162521{
Howard Hinnant2c39cbe2013-06-27 19:35:322522 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162523 {
2524 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2525 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:322526 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:162527 this->__size_ = this->__cap() = 0;
2528 }
2529}
2530
2531template <class _Allocator>
2532typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:402533vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162534{
2535 size_type __amax = __storage_traits::max_size(__alloc());
2536 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2537 if (__nmax / __bits_per_word <= __amax)
2538 return __nmax;
2539 return __internal_cap_to_external(__amax);
2540}
2541
2542// Precondition: __new_size > capacity()
2543template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002544inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162545typename vector<bool, _Allocator>::size_type
2546vector<bool, _Allocator>::__recommend(size_type __new_size) const
2547{
2548 const size_type __ms = max_size();
2549 if (__new_size > __ms)
2550 this->__throw_length_error();
2551 const size_type __cap = capacity();
2552 if (__cap >= __ms / 2)
2553 return __ms;
Howard Hinnant7f764502013-08-14 18:00:202554 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:162555}
2556
2557// Default constructs __n objects starting at __end_
2558// Precondition: __n > 0
2559// Precondition: size() + __n <= capacity()
2560// Postcondition: size() == size() + __n
2561template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002562inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162563void
2564vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2565{
2566 size_type __old_size = this->__size_;
2567 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:192568 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162569}
2570
2571template <class _Allocator>
2572template <class _ForwardIterator>
2573typename enable_if
2574<
2575 __is_forward_iterator<_ForwardIterator>::value,
2576 void
2577>::type
2578vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2579{
2580 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:192581 this->__size_ += _VSTD::distance(__first, __last);
2582 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:162583}
2584
2585template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002586inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162587vector<bool, _Allocator>::vector()
Marshall Clowc912c0c2015-06-04 02:05:412588 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:322589 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162590 __size_(0),
2591 __cap_alloc_(0)
2592{
2593}
2594
2595template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002596inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162597vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:202598#if _LIBCPP_STD_VER <= 14
2599 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2600#else
2601 _NOEXCEPT
2602#endif
Howard Hinnant2c39cbe2013-06-27 19:35:322603 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162604 __size_(0),
2605 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2606{
2607}
2608
2609template <class _Allocator>
2610vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:322611 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162612 __size_(0),
2613 __cap_alloc_(0)
2614{
2615 if (__n > 0)
2616 {
2617 allocate(__n);
2618 __construct_at_end(__n, false);
2619 }
2620}
2621
Marshall Clowa49a2c92013-09-14 00:47:592622#if _LIBCPP_STD_VER > 11
2623template <class _Allocator>
2624vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2625 : __begin_(nullptr),
2626 __size_(0),
2627 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2628{
2629 if (__n > 0)
2630 {
2631 allocate(__n);
2632 __construct_at_end(__n, false);
2633 }
2634}
2635#endif
2636
Howard Hinnantbc8d3f92010-05-11 19:42:162637template <class _Allocator>
2638vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:322639 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162640 __size_(0),
2641 __cap_alloc_(0)
2642{
2643 if (__n > 0)
2644 {
2645 allocate(__n);
2646 __construct_at_end(__n, __x);
2647 }
2648}
2649
2650template <class _Allocator>
2651vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322652 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162653 __size_(0),
2654 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2655{
2656 if (__n > 0)
2657 {
2658 allocate(__n);
2659 __construct_at_end(__n, __x);
2660 }
2661}
2662
2663template <class _Allocator>
2664template <class _InputIterator>
2665vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2666 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2667 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322668 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162669 __size_(0),
2670 __cap_alloc_(0)
2671{
2672#ifndef _LIBCPP_NO_EXCEPTIONS
2673 try
2674 {
Howard Hinnant324bb032010-08-22 00:02:432675#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162676 for (; __first != __last; ++__first)
2677 push_back(*__first);
2678#ifndef _LIBCPP_NO_EXCEPTIONS
2679 }
2680 catch (...)
2681 {
Howard Hinnant2c39cbe2013-06-27 19:35:322682 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162683 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2684 __invalidate_all_iterators();
2685 throw;
2686 }
Howard Hinnant324bb032010-08-22 00:02:432687#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162688}
2689
2690template <class _Allocator>
2691template <class _InputIterator>
2692vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2693 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2694 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322695 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162696 __size_(0),
2697 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2698{
2699#ifndef _LIBCPP_NO_EXCEPTIONS
2700 try
2701 {
Howard Hinnant324bb032010-08-22 00:02:432702#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162703 for (; __first != __last; ++__first)
2704 push_back(*__first);
2705#ifndef _LIBCPP_NO_EXCEPTIONS
2706 }
2707 catch (...)
2708 {
Howard Hinnant2c39cbe2013-06-27 19:35:322709 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162710 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2711 __invalidate_all_iterators();
2712 throw;
2713 }
Howard Hinnant324bb032010-08-22 00:02:432714#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162715}
2716
2717template <class _Allocator>
2718template <class _ForwardIterator>
2719vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2720 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322721 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162722 __size_(0),
2723 __cap_alloc_(0)
2724{
Howard Hinnant0949eed2011-06-30 21:18:192725 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:162726 if (__n > 0)
2727 {
2728 allocate(__n);
2729 __construct_at_end(__first, __last);
2730 }
2731}
2732
2733template <class _Allocator>
2734template <class _ForwardIterator>
2735vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2736 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322737 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162738 __size_(0),
2739 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2740{
Howard Hinnant0949eed2011-06-30 21:18:192741 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:162742 if (__n > 0)
2743 {
2744 allocate(__n);
2745 __construct_at_end(__first, __last);
2746 }
2747}
2748
Eric Fiselierad421ef2017-04-16 02:40:452749#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:022750
Howard Hinnantbc8d3f92010-05-11 19:42:162751template <class _Allocator>
2752vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:322753 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162754 __size_(0),
2755 __cap_alloc_(0)
2756{
2757 size_type __n = static_cast<size_type>(__il.size());
2758 if (__n > 0)
2759 {
2760 allocate(__n);
2761 __construct_at_end(__il.begin(), __il.end());
2762 }
2763}
2764
2765template <class _Allocator>
2766vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322767 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162768 __size_(0),
2769 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2770{
2771 size_type __n = static_cast<size_type>(__il.size());
2772 if (__n > 0)
2773 {
2774 allocate(__n);
2775 __construct_at_end(__il.begin(), __il.end());
2776 }
2777}
2778
Eric Fiselierad421ef2017-04-16 02:40:452779#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:022780
Howard Hinnantbc8d3f92010-05-11 19:42:162781template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162782vector<bool, _Allocator>::~vector()
2783{
Howard Hinnant2c39cbe2013-06-27 19:35:322784 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162785 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:162786 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:162787}
2788
2789template <class _Allocator>
2790vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:322791 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162792 __size_(0),
2793 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2794{
2795 if (__v.size() > 0)
2796 {
2797 allocate(__v.size());
2798 __construct_at_end(__v.begin(), __v.end());
2799 }
2800}
2801
2802template <class _Allocator>
2803vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322804 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162805 __size_(0),
2806 __cap_alloc_(0, __a)
2807{
2808 if (__v.size() > 0)
2809 {
2810 allocate(__v.size());
2811 __construct_at_end(__v.begin(), __v.end());
2812 }
2813}
2814
2815template <class _Allocator>
2816vector<bool, _Allocator>&
2817vector<bool, _Allocator>::operator=(const vector& __v)
2818{
2819 if (this != &__v)
2820 {
2821 __copy_assign_alloc(__v);
2822 if (__v.__size_)
2823 {
2824 if (__v.__size_ > capacity())
2825 {
2826 deallocate();
2827 allocate(__v.__size_);
2828 }
Howard Hinnant0949eed2011-06-30 21:18:192829 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:162830 }
2831 __size_ = __v.__size_;
2832 }
2833 return *this;
2834}
2835
Eric Fiselierad421ef2017-04-16 02:40:452836#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant73d21a42010-09-04 23:28:192837
Howard Hinnantbc8d3f92010-05-11 19:42:162838template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002839inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162840vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:322841#if _LIBCPP_STD_VER > 14
2842 _NOEXCEPT
2843#else
Howard Hinnantd1d27a42011-06-03 19:40:402844 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:322845#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162846 : __begin_(__v.__begin_),
2847 __size_(__v.__size_),
2848 __cap_alloc_(__v.__cap_alloc_)
2849{
Howard Hinnant2c39cbe2013-06-27 19:35:322850 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:162851 __v.__size_ = 0;
2852 __v.__cap() = 0;
2853}
2854
2855template <class _Allocator>
2856vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322857 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162858 __size_(0),
2859 __cap_alloc_(0, __a)
2860{
2861 if (__a == allocator_type(__v.__alloc()))
2862 {
2863 this->__begin_ = __v.__begin_;
2864 this->__size_ = __v.__size_;
2865 this->__cap() = __v.__cap();
2866 __v.__begin_ = nullptr;
2867 __v.__cap() = __v.__size_ = 0;
2868 }
2869 else if (__v.size() > 0)
2870 {
2871 allocate(__v.size());
2872 __construct_at_end(__v.begin(), __v.end());
2873 }
2874}
2875
2876template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002877inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162878vector<bool, _Allocator>&
2879vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:002880 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:162881{
2882 __move_assign(__v, integral_constant<bool,
2883 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:452884 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:162885}
2886
2887template <class _Allocator>
2888void
2889vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2890{
2891 if (__alloc() != __c.__alloc())
2892 assign(__c.begin(), __c.end());
2893 else
2894 __move_assign(__c, true_type());
2895}
2896
2897template <class _Allocator>
2898void
2899vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402900 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162901{
2902 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:152903 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:162904 this->__begin_ = __c.__begin_;
2905 this->__size_ = __c.__size_;
2906 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:162907 __c.__begin_ = nullptr;
2908 __c.__cap() = __c.__size_ = 0;
2909}
Howard Hinnant73d21a42010-09-04 23:28:192910
Eric Fiselierad421ef2017-04-16 02:40:452911#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:162912
2913template <class _Allocator>
2914void
2915vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2916{
2917 __size_ = 0;
2918 if (__n > 0)
2919 {
2920 size_type __c = capacity();
2921 if (__n <= __c)
2922 __size_ = __n;
2923 else
2924 {
2925 vector __v(__alloc());
2926 __v.reserve(__recommend(__n));
2927 __v.__size_ = __n;
2928 swap(__v);
2929 }
Howard Hinnant0949eed2011-06-30 21:18:192930 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162931 }
Eric Fiselierfb342382016-12-28 06:06:092932 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:162933}
2934
2935template <class _Allocator>
2936template <class _InputIterator>
2937typename enable_if
2938<
2939 __is_input_iterator<_InputIterator>::value &&
2940 !__is_forward_iterator<_InputIterator>::value,
2941 void
2942>::type
2943vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2944{
2945 clear();
2946 for (; __first != __last; ++__first)
2947 push_back(*__first);
2948}
2949
2950template <class _Allocator>
2951template <class _ForwardIterator>
2952typename enable_if
2953<
2954 __is_forward_iterator<_ForwardIterator>::value,
2955 void
2956>::type
2957vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2958{
2959 clear();
Eric Fiselier0e5ebbc2016-12-23 23:37:522960 difference_type __ns = _VSTD::distance(__first, __last);
2961 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2962 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantbc8d3f92010-05-11 19:42:162963 if (__n)
2964 {
2965 if (__n > capacity())
2966 {
2967 deallocate();
2968 allocate(__n);
2969 }
2970 __construct_at_end(__first, __last);
2971 }
2972}
2973
2974template <class _Allocator>
2975void
2976vector<bool, _Allocator>::reserve(size_type __n)
2977{
2978 if (__n > capacity())
2979 {
2980 vector __v(this->__alloc());
2981 __v.allocate(__n);
2982 __v.__construct_at_end(this->begin(), this->end());
2983 swap(__v);
2984 __invalidate_all_iterators();
2985 }
2986}
2987
2988template <class _Allocator>
2989void
Howard Hinnantd1d27a42011-06-03 19:40:402990vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162991{
2992 if (__external_cap_to_internal(size()) > __cap())
2993 {
2994#ifndef _LIBCPP_NO_EXCEPTIONS
2995 try
2996 {
Howard Hinnant324bb032010-08-22 00:02:432997#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162998 vector(*this, allocator_type(__alloc())).swap(*this);
2999#ifndef _LIBCPP_NO_EXCEPTIONS
3000 }
3001 catch (...)
3002 {
3003 }
Howard Hinnant324bb032010-08-22 00:02:433004#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:163005 }
3006}
3007
3008template <class _Allocator>
3009typename vector<bool, _Allocator>::reference
3010vector<bool, _Allocator>::at(size_type __n)
3011{
3012 if (__n >= size())
3013 this->__throw_out_of_range();
3014 return (*this)[__n];
3015}
3016
3017template <class _Allocator>
3018typename vector<bool, _Allocator>::const_reference
3019vector<bool, _Allocator>::at(size_type __n) const
3020{
3021 if (__n >= size())
3022 this->__throw_out_of_range();
3023 return (*this)[__n];
3024}
3025
3026template <class _Allocator>
3027void
3028vector<bool, _Allocator>::push_back(const value_type& __x)
3029{
3030 if (this->__size_ == this->capacity())
3031 reserve(__recommend(this->__size_ + 1));
3032 ++this->__size_;
3033 back() = __x;
3034}
3035
3036template <class _Allocator>
3037typename vector<bool, _Allocator>::iterator
3038vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3039{
3040 iterator __r;
3041 if (size() < capacity())
3042 {
3043 const_iterator __old_end = end();
3044 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:193045 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163046 __r = __const_iterator_cast(__position);
3047 }
3048 else
3049 {
3050 vector __v(__alloc());
3051 __v.reserve(__recommend(__size_ + 1));
3052 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:193053 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3054 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163055 swap(__v);
3056 }
3057 *__r = __x;
3058 return __r;
3059}
3060
3061template <class _Allocator>
3062typename vector<bool, _Allocator>::iterator
3063vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3064{
3065 iterator __r;
3066 size_type __c = capacity();
3067 if (__n <= __c && size() <= __c - __n)
3068 {
3069 const_iterator __old_end = end();
3070 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:193071 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163072 __r = __const_iterator_cast(__position);
3073 }
3074 else
3075 {
3076 vector __v(__alloc());
3077 __v.reserve(__recommend(__size_ + __n));
3078 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193079 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3080 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163081 swap(__v);
3082 }
Howard Hinnant0949eed2011-06-30 21:18:193083 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163084 return __r;
3085}
3086
3087template <class _Allocator>
3088template <class _InputIterator>
3089typename enable_if
3090<
3091 __is_input_iterator <_InputIterator>::value &&
3092 !__is_forward_iterator<_InputIterator>::value,
3093 typename vector<bool, _Allocator>::iterator
3094>::type
3095vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3096{
3097 difference_type __off = __position - begin();
3098 iterator __p = __const_iterator_cast(__position);
3099 iterator __old_end = end();
3100 for (; size() != capacity() && __first != __last; ++__first)
3101 {
3102 ++this->__size_;
3103 back() = *__first;
3104 }
3105 vector __v(__alloc());
3106 if (__first != __last)
3107 {
3108#ifndef _LIBCPP_NO_EXCEPTIONS
3109 try
3110 {
Howard Hinnant324bb032010-08-22 00:02:433111#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:163112 __v.assign(__first, __last);
3113 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3114 difference_type __old_p = __p - begin();
3115 reserve(__recommend(size() + __v.size()));
3116 __p = begin() + __old_p;
3117 __old_end = begin() + __old_size;
3118#ifndef _LIBCPP_NO_EXCEPTIONS
3119 }
3120 catch (...)
3121 {
3122 erase(__old_end, end());
3123 throw;
3124 }
Howard Hinnant324bb032010-08-22 00:02:433125#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:163126 }
Howard Hinnant0949eed2011-06-30 21:18:193127 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163128 insert(__p, __v.begin(), __v.end());
3129 return begin() + __off;
3130}
3131
3132template <class _Allocator>
3133template <class _ForwardIterator>
3134typename enable_if
3135<
3136 __is_forward_iterator<_ForwardIterator>::value,
3137 typename vector<bool, _Allocator>::iterator
3138>::type
3139vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3140{
Eric Fiseliera2cd2702016-12-11 05:31:003141 const difference_type __n_signed = _VSTD::distance(__first, __last);
3142 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3143 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantbc8d3f92010-05-11 19:42:163144 iterator __r;
3145 size_type __c = capacity();
3146 if (__n <= __c && size() <= __c - __n)
3147 {
3148 const_iterator __old_end = end();
3149 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:193150 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163151 __r = __const_iterator_cast(__position);
3152 }
3153 else
3154 {
3155 vector __v(__alloc());
3156 __v.reserve(__recommend(__size_ + __n));
3157 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193158 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3159 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163160 swap(__v);
3161 }
Howard Hinnant0949eed2011-06-30 21:18:193162 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163163 return __r;
3164}
3165
3166template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003167inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163168typename vector<bool, _Allocator>::iterator
3169vector<bool, _Allocator>::erase(const_iterator __position)
3170{
3171 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:193172 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163173 --__size_;
3174 return __r;
3175}
3176
3177template <class _Allocator>
3178typename vector<bool, _Allocator>::iterator
3179vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3180{
3181 iterator __r = __const_iterator_cast(__first);
3182 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:193183 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163184 __size_ -= __d;
3185 return __r;
3186}
3187
3188template <class _Allocator>
3189void
3190vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:563191#if _LIBCPP_STD_VER >= 14
3192 _NOEXCEPT
3193#else
Eric Fiselierfb342382016-12-28 06:06:093194 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:563195 __is_nothrow_swappable<allocator_type>::value)
3196#endif
Howard Hinnantbc8d3f92010-05-11 19:42:163197{
Howard Hinnant0949eed2011-06-30 21:18:193198 _VSTD::swap(this->__begin_, __x.__begin_);
3199 _VSTD::swap(this->__size_, __x.__size_);
3200 _VSTD::swap(this->__cap(), __x.__cap());
Eric Fiselierad421ef2017-04-16 02:40:453201 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow7d914d12015-07-13 20:04:563202 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:163203}
3204
Howard Hinnant324bb032010-08-22 00:02:433205template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163206void
3207vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3208{
3209 size_type __cs = size();
3210 if (__cs < __sz)
3211 {
3212 iterator __r;
3213 size_type __c = capacity();
3214 size_type __n = __sz - __cs;
3215 if (__n <= __c && __cs <= __c - __n)
3216 {
3217 __r = end();
3218 __size_ += __n;
3219 }
3220 else
3221 {
3222 vector __v(__alloc());
3223 __v.reserve(__recommend(__size_ + __n));
3224 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193225 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:163226 swap(__v);
3227 }
Howard Hinnant0949eed2011-06-30 21:18:193228 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163229 }
3230 else
3231 __size_ = __sz;
3232}
3233
Howard Hinnant324bb032010-08-22 00:02:433234template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163235void
Howard Hinnantd1d27a42011-06-03 19:40:403236vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163237{
3238 // do middle whole words
3239 size_type __n = __size_;
3240 __storage_pointer __p = __begin_;
3241 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3242 *__p = ~*__p;
3243 // do last partial word
3244 if (__n > 0)
3245 {
3246 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3247 __storage_type __b = *__p & __m;
3248 *__p &= ~__m;
3249 *__p |= ~__b & __m;
3250 }
3251}
3252
Howard Hinnant324bb032010-08-22 00:02:433253template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163254bool
3255vector<bool, _Allocator>::__invariants() const
3256{
Howard Hinnant2c39cbe2013-06-27 19:35:323257 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:163258 {
3259 if (this->__size_ != 0 || this->__cap() != 0)
3260 return false;
3261 }
3262 else
3263 {
3264 if (this->__cap() == 0)
3265 return false;
3266 if (this->__size_ > this->capacity())
3267 return false;
3268 }
3269 return true;
3270}
3271
Howard Hinnant324bb032010-08-22 00:02:433272template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163273size_t
Howard Hinnantd1d27a42011-06-03 19:40:403274vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163275{
3276 size_t __h = 0;
3277 // do middle whole words
3278 size_type __n = __size_;
3279 __storage_pointer __p = __begin_;
3280 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3281 __h ^= *__p;
3282 // do last partial word
3283 if (__n > 0)
3284 {
3285 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3286 __h ^= *__p & __m;
3287 }
3288 return __h;
3289}
3290
3291template <class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:003292struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:163293 : public unary_function<vector<bool, _Allocator>, size_t>
3294{
Howard Hinnantee6ccd02010-09-23 18:58:283295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:403296 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163297 {return __vec.__hash_code();}
3298};
3299
3300template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163302bool
3303operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3304{
3305 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:193306 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:163307}
3308
3309template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003310inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163311bool
3312operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3313{
3314 return !(__x == __y);
3315}
3316
3317template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003318inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163319bool
3320operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3321{
Howard Hinnant0949eed2011-06-30 21:18:193322 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163323}
3324
3325template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003326inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163327bool
3328operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3329{
3330 return __y < __x;
3331}
3332
3333template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003334inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163335bool
3336operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3337{
3338 return !(__x < __y);
3339}
3340
3341template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163343bool
3344operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3345{
3346 return !(__y < __x);
3347}
3348
3349template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003350inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163351void
3352swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:403353 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:163354{
3355 __x.swap(__y);
3356}
3357
3358_LIBCPP_END_NAMESPACE_STD
3359
3360#endif // _LIBCPP_VECTOR